package org.hdl.anima.message;

import java.io.IOException;

import org.hdl.anima.common.io.InputArchive;
import org.hdl.anima.common.io.OutputArchive;

/**
 * @author qiuhd
 * @since  2014-2-17
 * @version V1.0.0
 */
public abstract class Request implements IRequest {
	/**
	 * 消息Id
	 */
	private int id ;
	/**
	 * 请求消息序列号
	 */
	private int seq ;
	
	private boolean twoWay;
	
	private boolean encrypted = false;
	
	public Request() {
		this(0);
	}
	
	public Request(int id) {
		this.id = id;
	}
	
	@Override
	public void serialize(OutputArchive archive) throws IOException {
		archive.writeInt(id);
		archive.writeInt(seq);
		archive.writeBool(twoWay);
		doSerialize(archive);
	}
	
	@Override
	public void deserialize(InputArchive archive) throws IOException {
		this.seq = archive.readInt();
		this.twoWay = archive.readBool();
		doDeserialize(archive);
	}
	
	public abstract void doSerialize(OutputArchive out) throws IOException;
	
	public abstract void doDeserialize(InputArchive in) throws IOException;
	
	public void display(StringBuilder builder,int level) {
		MDisplayer displayer = new MDisplayer(builder);
		displayer.display(this.id, "id");
		displayer.display(this.seq, "seq");
		displayer.display(this.twoWay, "twoWay");
		doDisplay(displayer);
	}
	
	public void displaySimple(StringBuilder builder,int level) {
		MDisplayer displayer = new MDisplayer(builder);
		displayer.displaySimple(this.id, true);
		displayer.displaySimple(this.seq, true);
		displayer.displaySimple(this.twoWay, true);
		displaySimple(displayer);
	}
	
	public abstract void doDisplay(MDisplayer displayer) ;
	
	public abstract void displaySimple(MDisplayer displayer) ;
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public int getSeq() {
		return seq;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}

	public boolean isTwoWay() {
		return twoWay;
	}

	public void setTwoWay(boolean twoWay) {
		this.twoWay = twoWay;
	}

	@Override
	public boolean isEncrypted() {
		return this.encrypted;
	}

	@Override
	public void setEncrypted(boolean encrypted) {
		this.encrypted = encrypted;
	}
}

